package com.Babysleep.manage;

import java.io.File;
import java.io.IOException;

import android.media.MediaRecorder;
import android.os.Environment;

import com.Babysleep.Activity.MaMaShuo;

/**
 * 完成录音动作
 * 
 */
public class Record {
	private MediaRecorder mediaRecorder;
	public boolean Is_Recording = false;
	private long name = 0;

	public Record(MaMaShuo activity) {
		try {
			mediaRecorder = new MediaRecorder();
			mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
			mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
			mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 开始录音
	 */
	public long start() {
		try {
			if (Is_Recording == false) {
				name = System.currentTimeMillis();
				File file=new File(Environment
						.getExternalStorageDirectory()
						+ "/mamashuo"
						);
				if (!file.exists()) {
					file.mkdir();
				}
				mediaRecorder.setOutputFile(Environment
						.getExternalStorageDirectory()
						+ "/mamashuo/"
						+ String.valueOf(name) + ".amr");

				mediaRecorder.prepare();
				mediaRecorder.start();
				Is_Recording = true;
				return name;
			}
			return 0;
		} catch (IllegalStateException e) {
			e.printStackTrace();
			return -1;
		} catch (IOException e) {
			e.printStackTrace();
			return -2;
		}
	}

	/**
	 * 停止录音
	 */
	public void stop() {
		if (Is_Recording == true) {
			mediaRecorder.stop();
			mediaRecorder.release();
			mediaRecorder=null;
			Is_Recording = false;
		}
	}

}
